package com.google.code.jmarias.client.states;

import com.google.code.jmarias.client.Entity;
import com.google.code.jmarias.client.Globals;
import com.google.code.jmarias.client.components.ImageRenderComponent;
import com.google.code.jmarias.client.components.ButtonRenderComponent;
import java.util.ArrayList;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;


/**
 * Obrazovka Hlavni Menu
 * @author bart
 */
public class MainMenuState extends AbstractState {
    private int stateID = -1;

    private static boolean isRestarting = false;



    /* Definice entit */
    private ArrayList<Entity> entitiesArr = new ArrayList<Entity>();


    /* Inicializace obrazku */
    private ArrayList<Image> imgArr = new ArrayList<Image>();
    
    private ArrayList<Image> buttonImgNormalArr = new ArrayList<Image>();
    private ArrayList<Image> buttonImgHoverArr = new ArrayList<Image>();

    

    /** Souradnice leveho horniho rohu menu.
     *  - na vysku pocita se 3 + 0.5 tlacitky!
     *  - sirku centruje v ramci 800x600 rozliseni
     */
    private Vector2f menuPosition = new Vector2f();

    
    public MainMenuState (int stateID, Globals global) throws SlickException {
        this.stateID = stateID;
        this.globals = global;

        menuPosition = new Vector2f((global.getScreenWidth()-640) /2 ,(global.getScreenHeight() - 400) / 2);

    }

    public int getID() {
        return globals.getMAINMENUSTATE();

    }

    public static void setRestarting(boolean restart) {
        isRestarting = restart;
    }


    public void init(GameContainer gc, StateBasedGame sb) throws SlickException {
        setRestarting(false);
//        super.loadFonts();
        
        /* Nahrani mapy cudliku */
        imgArr.add(new Image(globals.getIMGPATH() + "menuCudliky.png"));

        /* Rozsekani mapy do jednotlivych casti */
        // Tlacitko Start
        buttonImgNormalArr.add(imgArr.get(0).getSubImage(0, 0, 200, 100));
        buttonImgHoverArr.add(imgArr.get(0).getSubImage(200, 0, 200, 100));

        // Tlacitko Restart
        buttonImgNormalArr.add(imgArr.get(0).getSubImage(0, 100, 200, 100));
        buttonImgHoverArr.add(imgArr.get(0).getSubImage(200, 100, 200, 100));
        
        // Tlacitko Nic
        buttonImgNormalArr.add(imgArr.get(0).getSubImage(0, 300, 200, 100));
        buttonImgHoverArr.add(imgArr.get(0).getSubImage(200, 300, 200, 100));

        // Tlacitko Konec
        buttonImgNormalArr.add(imgArr.get(0).getSubImage(0, 200, 200, 100));
        buttonImgHoverArr.add(imgArr.get(0).getSubImage(200, 200, 200, 100));




        /* Vytvoreni entity pozadi pro hlavni menu */
        entitiesArr.add(new Entity("background"));
        entitiesArr.get(0).setScale(globals.getGlobalScale().x);
        entitiesArr.get(0).AddComponent(new ImageRenderComponent(0, new Image(globals.getIMGPATH() + "pozadiMenu.png")));

        /* Logo */
        entitiesArr.add(new Entity("imgLogo"));
        entitiesArr.get(1).AddComponent(new ImageRenderComponent(0, new Image(globals.getIMGPATH() + "prototyp-menu.png")));
        entitiesArr.get(1).setPosition(new Vector2f(menuPosition.x + 240, menuPosition.y));


        /* Vytvoreni tlacitka Start - inicializace entity, nahrani obrazku, urceni pozice */
        entitiesArr.add(new Entity("buttonStart"));
        entitiesArr.get(2).AddComponent(new ButtonRenderComponent(0, globals, buttonImgNormalArr.get(0), buttonImgHoverArr.get(0)));
        entitiesArr.get(2).setPosition(menuPosition);

        /* Restart */
        entitiesArr.add(new Entity("buttonRestart"));
        entitiesArr.get(3).AddComponent(new ButtonRenderComponent(1, globals, buttonImgNormalArr.get(1), buttonImgHoverArr.get(1)));
        entitiesArr.get(3).setPosition(new Vector2f(menuPosition.x,menuPosition.y+100));

        /* Nic nedelajici tlačítko */
        entitiesArr.add(new Entity("buttonNothing"));
        entitiesArr.get(4).AddComponent(new ButtonRenderComponent(-2, globals, buttonImgNormalArr.get(2), buttonImgHoverArr.get(2)));
        entitiesArr.get(4).setPosition(new Vector2f(menuPosition.x,menuPosition.y+200));

        /* Exit */
        entitiesArr.add(new Entity("buttonExit"));
        entitiesArr.get(5).AddComponent(new ButtonRenderComponent(2, globals, buttonImgNormalArr.get(3), buttonImgHoverArr.get(3)));
        entitiesArr.get(5).setPosition(new Vector2f(menuPosition.x,menuPosition.y+300));


        /* Vytvoreni waiting obrazku */
        entitiesArr.add(new Entity ("imgWait"));
        entitiesArr.get(6).AddComponent(new ImageRenderComponent(0, new Image(globals.getIMGPATH() + "cekaci.png")));
        entitiesArr.get(6).setPosition(new Vector2f((globals.getScreenWidth() - 50) / 2, (globals.getScreenHeight() - 50) / 2));


    }

    public void render(GameContainer gc, StateBasedGame sb, Graphics gr) throws SlickException {

        for(int i = 0; i < entitiesArr.size(); i++) {
            if((i == 6 && isRestarting) || (i != 6)) {
                entitiesArr.get(i).render(gc,null,gr);
            }
        }
    }

    public void update(GameContainer gc, StateBasedGame sb, int delta) throws SlickException {

        Input input = gc.getInput();
        input.enableKeyRepeat();

        int mouseX = input.getMouseX();
        int mouseY = input.getMouseY();

        Vector2f [] buttonPosition = { entitiesArr.get(2).getPosition(),
                                        entitiesArr.get(3).getPosition(),
                                        entitiesArr.get(5).getPosition() };

        
        Vector2f posStart = entitiesArr.get(2).getPosition();
        Vector2f posRestart = entitiesArr.get(3).getPosition();
        Vector2f posExit = entitiesArr.get(5).getPosition();


        if(isRestarting) {
            gc.reinit();
            sb.enterState(globals.getGAMESTATE());
            globals.setHoverID(0);
        }

        /* Pokud se mys nachazi nad nekterym z menu tlacitek zvyraznim ho. */
        if (( mouseX >= buttonPosition[0].x && mouseX <= buttonPosition[0].x + buttonImgNormalArr.get(0).getWidth()) &&
            ( mouseY >= buttonPosition[0].y && mouseY <= buttonPosition[0].y + buttonImgNormalArr.get(0).getHeight() )) {
            globals.setHoverID(1);

        } else if ((mouseX >= posRestart.x && mouseX <= posRestart.x + buttonImgNormalArr.get(1).getWidth()) &&
                  (mouseY >= posRestart.y && mouseY <= posRestart.y + buttonImgNormalArr.get(1).getHeight())) {
            globals.setHoverID(2);

        } else if ((mouseX >= posExit.x && mouseX <= posExit.x + buttonImgNormalArr.get(3).getWidth()) &&
                   (mouseY >= posExit.y && mouseY <= posExit.y + buttonImgNormalArr.get(3).getHeight())) {
            globals.setHoverID(3);
        }


        /* Obsluha klavesnice a kliknuti mysi. */
        if(input.isKeyPressed(Input.KEY_DOWN)) {
            globals.setHoverID((globals.getHoverID() + 4 + 1) % 4);
        } else if(input.isKeyPressed(Input.KEY_UP)) {
            globals.setHoverID((globals.getHoverID() + 4 - 1) % 4);
        } else if(input.isKeyPressed(Input.KEY_ENTER) || input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
            switch (globals.getHoverID()) {
                case 1:
                    sb.enterState(globals.getGAMESTATE());
                    globals.setHoverID(0);
                    break;
                case 2:
                    isRestarting = true;
                    break;
                case 3:
                    gc.exit();
                    break;
                default:
                    break;
            }
        } else if(input.isKeyPressed(Input.KEY_ESCAPE)) {
            gc.exit();
        }

        /* Vsem ostatnim tlacitku vycisti prirazene akce */
        super.nullControls(gc);


        for(int i = 0; i < entitiesArr.size(); i++) {
            entitiesArr.get(i).update(gc,sb,delta);
        }

    }
}
